<?php
namespace app\admin\controller;
use think\facade\Db;
use app\admin\model\RegionModel;
use PhpOffice\PhpSpreadsheet\IOFactory;

class xingzhengqu  {

    public function getList() {
        $where = [];
        if(isset($_GET['pcode'])) {
            $where['pcode'] = $_GET['pcode'];
        } else {
            $where['pcode'] = null;
        }
        $list = Db::table('yhq_area')->where($where)->select();
        return array('list'=>$list);
    }

    public function getCity() {
        $where = [];
        if(isset($_GET['name'])) {
            $where['name'] = $_GET['name'];
        } else {
            $where['pcode'] = null;
        }
        $where['level'] = 2;
        $list = Db::table('yhq_area')->where($where)->find();
        return array('data'=>$list);
    }

    public function getCounty() {
        $where = [];
        if(isset($_GET['name'])) {
            $where['name'] = $_GET['name'];
        } else {
            $where['pcode'] = null;
        }
        $where['level'] = 3;
        $list = Db::table('yhq_area')->where($where)->find();
        
        $towns = Db::table('yhq_area')->field('code')->where('pcode','=',$list['code'])->select();
        $town_codes = array();
        foreach($towns as $value) {
            $town_codes[]=$value['code'];
        }
        $geos = Db::table('yhq_town_geo_json')->where('region_code','in',$town_codes)->select();
        $result = array();
        foreach($geos as $value) {
            $data = json_decode($value['geo_json'],true);
            $data['properties']['name'] = $data['properties']['Name'];
            $result[] = $data;
        }
        return array('features'=>$result,'type'=>'FeatureCollection');
    }
}